/*
	This file is part of Floculate.
	Copyright (C) 2008  Bill Whitacre

	Floculate is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Floculate is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "draw.h"
#include <math.h>

static float _r,_g,_b,_a;
int rat_pretty_draw=0;

void rat_line_width(float width)
{
	glLineWidth(width);
}

void rat_point_size(float size)
{
	glPointSize(size);
}

void rat_set_color(float r,float g,float b,float a)
{
	glColor4f(_r=r,_g=g,_b=b,_a=a);
}

void rat_get_color(float *r,float *g,float *b,float *a)
{
	*r=_r; *g=_g;
	*b=_b; *a=_a;
}

void rat_draw_point(float x,float y)
{
	glBegin(GL_POINTS);
		glVertex2d(x,y);
	glEnd();
}

void rat_draw_points(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_POINTS);
	{
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	}
	glEnd();
}

void rat_draw_line(float x1,float y1,float x2,float y2)
{
	glBegin(GL_LINES);
	{
		glVertex2d(x1,y1);
		glVertex2d(x2,y2);
	}
	glEnd();
}

void rat_draw_lines(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_LINES);
	{
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	}
	glEnd();
}

void rat_draw_line_strip(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_LINE_STRIP);
	{
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	}
	glEnd();
}

void rat_draw_line_loop(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_LINE_LOOP);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_quads(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_QUADS);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_triangles(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_TRIANGLES);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_triangle_fan(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_TRIANGLE_FAN);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_triangle_strip(float *points,unsigned int numpts)
{
	register unsigned int i;
	glBegin(GL_TRIANGLE_STRIP);
		for (i=0; i<numpts; i++)
			glVertex2d(points[i*2],points[i*2+1]);
	glEnd();
}

void rat_draw_rect(float x1,float y1,float x2,float y2,int fill)
{
	if (fill)
		glBegin(GL_QUADS);
	else
		glBegin(GL_LINE_LOOP);
	{
		glVertex2d(x1,y1);
		glVertex2d(x1,y2);
		glVertex2d(x2,y2);
		glVertex2d(x2,y1);
	} glEnd();
}

#ifndef _MSC_VER
#	define cos cosf
#	define sin sinf
#endif

void rat_draw_circle(float x,float y,float radius,float stepdivisor,int fill)
{
	float theta,xmod,ymod,step=MATH_PI/(4.0f*radius*stepdivisor);
	step=step>MATH_PI/4.0f?MATH_PI/4.0f:step;

	if (fill)
	{
		glBegin(GL_POLYGON);
			for (theta=0.0f; theta<=MATH_PI*2.0f; theta+=step)
			{
				xmod=cos(theta)*radius;
				ymod=sin(theta)*radius;
				glVertex2f(x+xmod,y+ymod);
			}
		glEnd();
	}
	else
	{
		glBegin(GL_LINE_STRIP);
			for (theta=0.0f; theta<MATH_PI*2.0f; theta+=step)
			{
				xmod=cos(theta)*radius;
				ymod=sin(theta)*radius;
				glVertex2f(x+xmod,y+ymod);
			}
			glVertex2f(x+xmod,y+ymod);
			glVertex2f(x+cos(0)*radius,y+sin(0)*radius);
		glEnd();
	}
}

void rat_draw_elipse(float x,float y,float rx,float ry,float stepdivisor,int fill)
{
	float theta,xmod,ymod,step=MATH_PI/(4.0f*(rx<ry?rx:ry)*stepdivisor);

	if (fill)
	{
		glBegin(GL_POLYGON);
			for (theta=0.0f; theta<=MATH_PI*2.0f; theta+=step)
			{
				xmod=cos(theta)*rx;
				ymod=sin(theta)*ry;
				glVertex2f(x+xmod,y+ymod);
			}
		glEnd();
	}
	else
	{
		glBegin(GL_LINE_STRIP);
			for (theta=0.0f; theta<MATH_PI*2.0f; theta+=step)
			{
				xmod=cos(theta)*rx;
				ymod=sin(theta)*ry;
				glVertex2f(x+xmod,y+ymod);
			}
			glVertex2f(x+xmod,y+ymod);
			glVertex2f(x+cos(0)*rx,y+sin(0)*ry);
		glEnd();
	}
}
